

<template>
  <div id="app">
    <!-- 查询表单 -->
    <b>筛选条件：</b>
<!--    欢迎{{this.user.username}}登录-->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">

      <el-form-item label="角色名称：">
        <el-input v-model="formInline.name" placeholder="请输入昵称"></el-input>
      </el-form-item>

      <el-form-item label="状态：">
        <el-select v-model="formInline.status" placeholder="请选择" clearable>
          <el-option label="全部" value=""></el-option>
          <el-option label="启用" value="0"></el-option>
          <el-option label="禁用" value="1"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="findPage()">查询</el-button>
        <el-button type="primary" @click="resetForm2()">清空</el-button>
      </el-form-item>
    </el-form>

    <!--表格用来展示数据-->
    <b>数据列表:</b>

    <el-button type="info" @click="toAdd()" plain>添加角色</el-button>
    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column
          prop="id"
          label="序号"
      >
      </el-table-column>

      <el-table-column
          prop="name"
          label="角色名称"
      >
      </el-table-column>

      <el-table-column
          prop="ms"
          label="角色描述"
      >
      </el-table-column>

      <el-table-column
          prop="qnum"
          label="用户数"
      >
      </el-table-column>

      <el-table-column
          prop="createTime"
          label="创建时间"
      >
      </el-table-column>

      <el-table-column
          prop="status"
          label="状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status==0" type="success">启用</el-tag>
          <el-tag v-else>禁用</el-tag>
        </template>
      </el-table-column>

      <el-table-column
          label="操作">
        <template slot-scope="scope">
<!--          <el-button type="text" @click="tc(scope.row.id)" size="mini">功能权限</el-button>-->
          <el-button type="text" @click="toUpdate(scope.row.id)" size="mini">修改</el-button>
          <el-button type="text" @click="del(scope.row.id)" size="mini">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页展示数据-->
    <el-pagination
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[3, 6, 9, 12]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <!--添加弹窗-->
    <el-dialog
        title="添加"
        :visible.sync="dialogVisible"
        width="30%">

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="角色名称:" prop="name">
          <el-input v-model="ruleForm.name" placeholder="请输入"></el-input>
        </el-form-item>

        <el-form-item label="角色标识:" prop="name">
          <el-input v-model="ruleForm.nameBs" placeholder="请输入"></el-input>
        </el-form-item>

        <el-form-item label="角色描述:" prop="name">
          <el-input v-model="ruleForm.ms" placeholder="请输入角色描述"></el-input>
        </el-form-item>

        <el-form-item label="所属应用端:" prop="name">
          <el-select v-model="ruleForm.appEnd" placeholder="请选择" clearable>
            <el-option label="来益移动端" value="来益移动端"></el-option>
            <el-option label="招行移动端" value="招行移动端"></el-option>
            <el-option label="工行移动端" value="工行移动端"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态:" prop="name">
          <el-select v-model="ruleForm.status" placeholder="请选择" clearable>
            <el-option label="启用" :value="0"></el-option>
            <el-option label="禁用" :value="1"></el-option>
          </el-select>
        </el-form-item>


        <el-form-item>
          <el-button type="primary" @click="add('ruleForm')" v-text="ruleForm.id==null?'添加':'修改'"></el-button>
          <el-button @click="resetForm()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

<!--    点击弹窗-->
    <el-dialog
        title="查看"
        :visible.sync="dialogVisible2"
        width="30%">

      <el-form :model="ruleForm2" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="ID:" prop="name">
          {{ruleForm2.number}}
        </el-form-item>

        <el-form-item label="昵称:" prop="name">
          {{ruleForm2.nickname}}
        </el-form-item>

        <el-form-item label="手机号:" prop="name">
          {{ruleForm2.phoneNumber}}
        </el-form-item>

        <el-form-item label="性别:" prop="name">
          {{ruleForm2.sex}}
        </el-form-item>

        <el-form-item label="用户角色:" prop="name">
          {{ruleForm2.userId}}
        </el-form-item>

        <el-form-item label="所属应用端:" prop="name">
          {{ruleForm2.appEnd}}
        </el-form-item>

        <el-form-item label="状态:" prop="name">
          {{ruleForm2.status==0?"锁定":ruleForm2.status==1?"有效":"注销"}}
        </el-form-item>

        <el-form-item label="创建时间:" prop="name">
          {{ruleForm2.createdAt}}
        </el-form-item>

        <el-form-item label="创建时间:" prop="name">
          {{ruleForm2.accountExpiryDate}}
        </el-form-item>

        <el-button type="primary" @click="guanbi()">关闭</el-button>
      </el-form>
    </el-dialog>

  </div>
</template>

<style >

</style>

<script>
// @ is an alias to /src
import axios from "axios"

export default {
  data(){
    return{
      /*
        data:就是用来定义数据的要用什么就自己定义什么
        []:数组，集合
        {}:对象(里面有属性)
         */
      tableData:[],
      pageNum:1,
      pageSize:3,
      total:0,
      formInline:{},
      user:{},
      roomList:[],
      dialogVisible:false,
      dialogVisible2:false,
      ruleForm:{},
      ruleForm2:{},
      imageUrl:"",
      inputMs:"",
    }
  },
  methods:{
    /*！！！！！！！！！！！
    methods:这个里面定义的全部都是方法
    现在是spring boot+vue 框架
    不一样的就是在 从vue框架里面的methods里面
    通过方法去请求路径(请求的路径就是后台的路径)
    拿到数据放到下面的created()里面来加载数据才能在页面展示

    ***html使用ajxa 请求controller  controller把数据返回给前端 不再去跳转页面了***

     */
    findPage(){ //查询所有加分页参数
      this.axios.post(`http://localhost:9006/yddqx/findPage?pageNum=${this.pageNum}&pageSize=${this.pageSize}`,this.formInline).then(res=>{
        console.log(res.data);
        this.tableData=res.data.data.records;
        this.total=res.data.data.total;
        if (this.pageNum>res.data.data.pages){
          this.pageNum=res.data.data.pages;
          this.findPage();
        }
      })
    },
    handleCurrentChange(val) {//分页方法
      this.pageNum=val;
      this.findPage();
    },
    toAdd(){ //往添加对话框里清空数据
      this.dialogVisible=true;
      this.ruleForm={};
    },

    add(formName) { //添加方法
      this.$refs[formName].validate((valid) => {
        if (valid) {//满足校验
          //通过ajax把数据保存到后端
            axios.post(`http://localhost:9006/yddqx/add`,this.ruleForm).then(res=>{
              if (res.data.code==200){
                this.$message.success("保存成功");
                this.dialogVisible=false;
              }else{
                this.$message.error("保存失败");
              }
              this.dialogVisible=false;
              this.findPage();
            })
        } else {//不满足校验
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm() { //重置按钮点击可以重置
      this.dialogVisible=false;
    },
    resetForm2(){
      this.formInline={};
    },
    toUpdate(id){ //修改回显数据方法
      axios.get(`http://localhost:9006/yddqx/findById?id=${id}`).then(res=>{
        this.ruleForm=res.data.data;
        this.dialogVisible=true;
      })
    },
    del(id){ //删除方法
      this.$confirm("确定要删除吗?一旦删除,该条信息将不能恢复。", '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.get(`http://localhost:9006/yddqx/deleteById?id=${id}`).then(res=>{
          if (res.data.code==200){
            this.$message.success("删除成功");
          }else{
            this.$message.error("删除失败！");
          }
          this.findPage();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    tc(id){
      this.dialogVisible2=true;
      this.axios.get(`http://localhost:9006/yddgl/findById?id=${id}`).then(res=>{
        this.ruleForm2=res.data.data;
      })
    },
    guanbi(){
      this.dialogVisible2=false;
    },
  },
  created(){
    /*
    ！！！！！！！！！！注意created()：
    这个里面才是可以把数据展示到页面的方法
    思考那个方法是要把数据放到前台 就在这个里面写那个方法
     */
    // var userDB=sessionStorage.getItem("userDB"); //从login.html页面的session域中拿到的数据
    // if (userDB==null){
    //   location.href="#/login";
    // }
    // this.user=JSON.parse(userDB);//动态获取显示登录人的数据 因为从session域中拿出来所以要转换数据类型
    this.findPage();//展示所有的数据
    // this.xialaList();//动态获取下拉框的数据
  }
}
</script>
